package C8_6_DuplicateObservedData;

import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.TextListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * Desc:&emsp;&ensp;复制监视GUI数据
 *
 * @author 李渊&emsp;
 * @version 1.0
 * @since V4.3&emsp;2022/7/13
 */
public class InternalWindow extends Frame {
    java.awt.TextField _startField;
    java.awt.TextField _endField;
    java.awt.TextField _lengthField;
    int curtFocusTextFieldFlag = 1;

    class TextValueListener implements TextListener {
        @Override
        public void textValueChanged(java.awt.event.TextEvent evt){
            Object obj = evt.getSource();
            if(obj == _startField && curtFocusTextFieldFlag ==1)
                StartField_TextChange(evt);
            else if(obj == _endField && curtFocusTextFieldFlag ==2)
                EndField_TextChange(evt);
            else if(obj == _lengthField && curtFocusTextFieldFlag ==3)
                LengthField_TextChange(evt);
        }
        void StartField_TextChange(java.awt.event.TextEvent evt){
            if(isNotInteger(_startField.getText()))
                _startField.setText("0");
            calculateLength();
        }
        void EndField_TextChange(java.awt.event.TextEvent evt){
            if(isNotInteger(_endField.getText()))
                _endField.setText("0");
            calculateLength();
        }
        void LengthField_TextChange(java.awt.event.TextEvent evt){
            if(isNotInteger(_lengthField.getText()))
                _lengthField.setText("0");
            calculateEnd();
        }
        boolean isNotInteger(String text){
            try {
                Integer.parseInt(text);
                return false;
            } catch (NumberFormatException e) {
                return true;
            }
        }
    }

    class TextFocusAdapter extends FocusAdapter {
        public void focusGained(java.awt.event.FocusEvent evt){
            Object obj = evt.getSource();
            if(obj == _startField)
                curtFocusTextFieldFlag =1;
            else if(obj == _endField)
                curtFocusTextFieldFlag =2;
            else if(obj == _lengthField)
                curtFocusTextFieldFlag =3;
        }
    }

    public InternalWindow() {
        super("InternalWindow");
        // 关闭
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        //需要设置可见性
        this.setVisible(true);
        //弹出的初始位置
        this.setLocation(600,500);
        //设置大小固定
        this.setResizable(false);
        this.setLayout(new GridLayout(3,2));
        // 添加输入框
        _startField = getTextField();
        _endField = getTextField();
        _lengthField = getTextField();

        TextFocusAdapter textFocusAdapter = new TextFocusAdapter();
        _startField.addFocusListener(textFocusAdapter);
        _endField.addFocusListener(textFocusAdapter);
        _lengthField.addFocusListener(textFocusAdapter);
        this.add(getLabel("Start："));
        this.add(_startField);
        this.add(getLabel("End："));
        this.add(_endField);
        this.add(getLabel("Length："));
        this.add(_lengthField);
        TextValueListener textValueListener = new TextValueListener();
        _startField.addTextListener(textValueListener);
        _endField.addTextListener(textValueListener);
        _lengthField.addTextListener(textValueListener);
        this.pack(); // Java函数，最优布局
        this.setVisible(true);
    }

    void calculateLength(){
        try{
            int start = Integer.parseInt(_startField.getText());
            int end = Integer.parseInt(_endField.getText());
            int length = end-start;
            _lengthField.setText(String.valueOf(length));
        } catch (NumberFormatException e){
            throw new RuntimeException("Unexpected Num Format Error!");
        }
    }
    void calculateEnd(){
        try{
            int start = Integer.parseInt(_startField.getText());
            int length = Integer.parseInt(_lengthField.getText());
            int end = start + length;
            _endField.setText(String.valueOf(end));
        } catch (NumberFormatException e){
            throw new RuntimeException("Unexpected Num Format Error!");
        }
    }

    private Label getLabel(String name){
        Font font= new Font(null,0, 100);
        Label label = new Label(name);
        label.setFont(font);
        return label;
    }

    private TextField getTextField(){
        Font font= new Font(null,0, 100);
        TextField textField = new TextField("0");
        textField.setFont(font);
        return textField;
    }
}
